const { DiffieHellmanGroup } = require('crypto')
var http = require('http')
var path = require('path')
var url = require('url')
var fsp = require('fs').promises

var { createServer } = http

var siteUrl = path.join(__dirname, "../site") // 站点默认路径
console.log(siteUrl)



createServer(function (req, res) {

    // req => request => 包含请求相关的信息
    // req.method => 请求的方式
    // req.url => 请求的url地址

    if (req.url != '/favicon.ico') {// 排除网站图标
        console.log('----------------------前端发送请求过来了------------------')
        var obj = url.parse(req.url)
        console.log(obj)
        console.log(obj.pathname)// 获取url中的文件路径


    

        fsp.readFile(path.join(siteUrl, obj.pathname), 'utf-8').then(data => {
            // console.log(data);
            // res.writeHead(200, {
            //     "Content-Type": "text/plain;charset=utf-8",
            // })
            res.end(data, 'utf-8')
        }).catch(err => {
            throw err;
        })

    }
}).listen(5000, function () {
    console.log("server is started at: localhost:5000");
})